
--region Drop Existing Procedures

IF OBJECT_ID(N'[dbo].[Lineups_Insert]') IS NOT NULL
	DROP PROCEDURE [dbo].[Lineups_Insert]

IF OBJECT_ID(N'[dbo].[Lineups_Update]') IS NOT NULL
	DROP PROCEDURE [dbo].[Lineups_Update]

IF OBJECT_ID(N'[dbo].[Lineups_Delete]') IS NOT NULL
	DROP PROCEDURE [dbo].[Lineups_Delete]

IF OBJECT_ID(N'[dbo].[Lineups_Select]') IS NOT NULL
	DROP PROCEDURE [dbo].[Lineups_Select]

--endregion

GO


--region [dbo].[Lineups_Select]

-- Create By: vdev124
-- Date Generated: Friday, March 04, 2011

CREATE PROCEDURE [dbo].[Lineups_Select]
	@Page int = null,
	@PageSize int = null,
    @SortBy nvarchar(255) = null,
	@SortType bit = null,
	@IdLineup int = null,
	@IdMatch int = null,
	@Lineup nvarchar(50) = null,
	@ClubLineup nvarchar(50) = null
AS

SET TRANSACTION ISOLATION LEVEL READ COMMITTED

Declare @RowCount int  
Set @RowCount = (
					Select Count (*) 
					From [dbo].[Lineups]
					WHERE
					(
						(@IdLineup is null OR [Lineups].[IdLineup] = @IdLineup)
						AND (@IdMatch is null OR [Lineups].[IdMatch] = @IdMatch)
						AND (@Lineup is null OR [Lineups].[Lineup] like @Lineup)
						AND (@ClubLineup is null OR [Lineups].[ClubLineup] like @ClubLineup)
					)
				)
if(@Page is null)
begin
	Set @Page = 1
end

if(@PageSize is null)
begin
	Set @PageSize = @RowCount
end

if(@SortBy is null)
begin
	Set @SortBy = 'DBNull'
end

if(@SortType is null)
begin
	Set @SortType = 1
end

SELECT *
FROM   (
			SELECT [dbo].[Lineups].*,
                   ROW_NUMBER() OVER 
                   (ORDER BY
                        CASE WHEN @SortBy = 'DBNull' THEN NewID() END ASC,
						CASE WHEN @SortBy = 'IdLineup' and @SortType = 1 THEN [Lineups].[IdLineup] END ASC,
						CASE WHEN @SortBy = 'IdLineup' and @SortType = 0 THEN [Lineups].[IdLineup] END DESC,
						CASE WHEN @SortBy = 'IdMatch' and @SortType = 1 THEN [Lineups].[IdMatch] END ASC,
						CASE WHEN @SortBy = 'IdMatch' and @SortType = 0 THEN [Lineups].[IdMatch] END DESC,
						CASE WHEN @SortBy = 'Lineup' and @SortType = 1 THEN [Lineups].[Lineup] END ASC,
						CASE WHEN @SortBy = 'Lineup' and @SortType = 0 THEN [Lineups].[Lineup] END DESC,
						CASE WHEN @SortBy = 'ClubLineup' and @SortType = 1 THEN [Lineups].[ClubLineup] END ASC,
						CASE WHEN @SortBy = 'ClubLineup' and @SortType = 0 THEN [Lineups].[ClubLineup] END DESC                   
                    ) AS RowNumber 
			FROM [dbo].[Lineups]
			Where 
			(
				(@IdLineup is null OR [Lineups].[IdLineup] = @IdLineup)
				AND (@IdMatch is null OR [Lineups].[IdMatch] = @IdMatch)
				AND (@Lineup is null OR [Lineups].[Lineup] like @Lineup)
				AND (@ClubLineup is null OR [Lineups].[ClubLineup] like @ClubLineup)
			)
		) AS Temp
WHERE  RowNumber Between (@Page * @PageSize - @PageSize + 1) And @Page * @PageSize
Return @RowCount


--end [dbo].[Lineups_Select]
--endregion

GO
--=========================================================================================--

	

--region [dbo].[Lineups_Insert]

-- Create By: vdev124
-- Date Generated: Friday, March 04, 2011

CREATE PROCEDURE [dbo].[Lineups_Insert]
	@IdLineup int OUTPUT,
	@IdMatch int,
	@Lineup nvarchar(50),
	@ClubLineup nvarchar(50)

AS


INSERT INTO [dbo].[Lineups] 
(
	[IdMatch],
	[Lineup],
	[ClubLineup]
)
VALUES 
(
	@IdMatch,
	@Lineup,
	@ClubLineup
)

SET @IdLineup = SCOPE_IDENTITY()

--end [dbo].[Lineups_Insert]
--endregion

GO
--=========================================================================================--

--region [dbo].[Lineups_Update]

-- Create By: vdev124
-- Date Generated: Friday, March 04, 2011

CREATE PROCEDURE [dbo].[Lineups_Update]
	@IdLineup int,
	@IdMatch int,
	@Lineup nvarchar(50),
	@ClubLineup nvarchar(50)
AS


UPDATE [dbo].[Lineups] SET
	[IdMatch] = @IdMatch,
	[Lineup] = @Lineup,
	[ClubLineup] = @ClubLineup
WHERE
	[IdLineup] = @IdLineup

--end [dbo].[Lineups_Update]
--endregion

GO
--=========================================================================================--

--region [dbo].[Lineups_Delete]

-- Create By: vdev124
-- Date Generated: Friday, March 04, 2011

CREATE PROCEDURE [dbo].[Lineups_Delete]
	@IdLineup int
AS


DELETE FROM [dbo].[Lineups]
WHERE
(
	[IdLineup] = @IdLineup
)

--end [dbo].[Lineups_Delete]
--endregion

GO
--=========================================================================================--

